package service

import (
	"context"
	"strconv"

	"gitee.com/thewalkers/refactoring-code/model"
	"gitee.com/thewalkers/refactoring-code/model/apperrors"
)

// SalariedClassificationService acts as a struct for injecting an implementation of EmployeeRepository
// for use in service methods
type SalariedClassificationService struct {
	ChangeSalariedTransactionRepository model.ChangeSalariedTransactionRepository
	PaymentClassificationRepository     model.PaymentClassificationRepository
}

// SCConfig will hold repository that will eventually be injected into this
// service layer
type SCConfig struct {
	ChangeSalariedTransactionRepository model.ChangeSalariedTransactionRepository
	PaymentClassificationRepository     model.PaymentClassificationRepository
}

// NewSalariedClassificationService is a factory function for
// initializing a EmployeeService with its repository layer dependencies
func NewSalariedClassificationService(s *SCConfig) model.ChangeSalariedTransactionService {
	return &SalariedClassificationService{
		ChangeSalariedTransactionRepository: s.ChangeSalariedTransactionRepository,
		PaymentClassificationRepository:     s.PaymentClassificationRepository,
	}
}

func (s *SalariedClassificationService) Update(ctx context.Context, id int64, salary *model.SalariedClassification) error {
	paymentClassification, err := s.PaymentClassificationRepository.FindByEmlpoyeeID(ctx, id)
	if err != nil {
		return apperrors.NewNotFound("paymentClassificationID", strconv.Itoa(int(id)))
	}

	salary.PaymentClassificationID = paymentClassification.ID

	err = s.ChangeSalariedTransactionRepository.Update(ctx, salary)

	return err
}
